﻿package com.sohu.sns.avatar {
	import flash.display.*;
	import flash.text.*;
	
	/**
	 * 缩略图显示
	 */
	public class PhotoThumb extends Sprite {
		private var thumbTitle : TextField;
		private var bmp : Bitmap;
		private var bg : Sprite;
		private var tWidth : Number;
		private var tHeight : Number;

		public function PhotoThumb(tWidth : Number = 80, tHeight : Number = 80, msg : String = "") {
			bmp = new Bitmap();
			this.tWidth = tWidth;
			this.tHeight = tHeight;
			this.bg = new Sprite();
			addChild(this.bg);
			if (msg.length > 0) {
				this.thumbTitle = new TextField();
				this.thumbTitle.text = msg;
				this.thumbTitle.textColor = 0x7B7B7B;
				this.thumbTitle.mouseEnabled = false;
				this.thumbTitle.autoSize = TextFieldAutoSize.LEFT;
				this.thumbTitle.x = 20;
				this.thumbTitle.y = 12;
				addChild(this.thumbTitle);
			}
		}

		public function draw(origin : BitmapData) : void {
			this.bmp = new Bitmap(origin, "never", true);
			addChild(this.bmp);
			this.bmp.x = 23;
			this.bmp.y = 35;
			this.reDrawBg();
		}

		public function display(origin : BitmapData) : void {
			this.bmp = new Bitmap(origin, "never", true);
			this.scaleBmp();
			addChild(this.bmp);
			this.bmp.x = 23;
			this.bmp.y = 35;
			this.reDrawBg();
		}

		private function reDrawBg() : void {
			this.bg.graphics.clear();
			this.bg.graphics.lineStyle(1, 0xE7ECF1);
			this.bg.graphics.beginFill(0xFFFFFF);
			this.bg.graphics.drawRect(0, 0, this.tWidth + 46, this.tHeight + 55);
			this.bg.graphics.endFill();
		}

		private function scaleBmp() : void {
			if (this.bmp.width != this.tWidth || this.bmp.height != this.tHeight) {
				var scalaWidth : Number = this.tWidth / this.bmp.width;
				var scalaHeight : Number = this.tHeight / this.bmp.height;
				var minScala : Number = scalaWidth < scalaHeight ? (scalaWidth) : (scalaHeight);
				this.bmp.smoothing = true;
				this.bmp.width = this.bmp.width * minScala;
				this.bmp.height = this.bmp.height * minScala;
			}
			this.bmp.x = (-(this.bmp.width - this.tWidth)) / 2;
		}
	}
}
